/*
 * NotaCadastrar.java
 *
 * Created on 13/02/2009, 14:25:29
 * Copyright Jaime Junior
 */
package Adecot.Notas;

import Adecot.*;
import Controller.EmpresaBO;
import Controller.Facade.IceDecorator;
import Controller.Facade.ValidaFacade;
import Controller.NotaProdutorBO;
import Model.BD.DAO;
import Model.Empresa;
import Model.NotaProdutor;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlMessage;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.ext.HtmlSelectOneRadio;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectedData;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.jsfcl.data.SelectInputDateBean;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.selectinputdate.SelectInputDate;
import com.sun.rave.faces.converter.CalendarConverter;
import com.sun.rave.faces.converter.SqlDateConverter;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.util.Date;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.CharacterConverter;
import javax.faces.convert.IntegerConverter;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class NotaCadastrar extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    private int __placeholder;
    NotaProdutor nota;
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private DefaultSelectionItems itemsBusca = new DefaultSelectionItems();

    public DefaultSelectionItems getItemsBusca() {
        return itemsBusca;
    }

    public void setItemsBusca(DefaultSelectionItems itemsBusca) {
        this.itemsBusca = itemsBusca;
    }
    private static boolean parar;
    private SelectInputDateBean dataSelecionada = new SelectInputDateBean();

    public SelectInputDateBean getDataSelecionada() {
        return dataSelecionada;
    }

    public void setDataSelecionada(SelectInputDateBean sidb) {
        this.dataSelecionada = sidb;
    }
    private DefaultSelectedData itemSelecionado = new DefaultSelectedData();

    public DefaultSelectedData getItemSelecionado() {
        return itemSelecionado;
    }

    public void setItemSelecionado(DefaultSelectedData dsd) {
        this.itemSelecionado = dsd;
    }
    private DefaultSelectionItems itens = new DefaultSelectionItems();

    public DefaultSelectionItems getItens() {
        return itens;
    }

    public void setItens(DefaultSelectionItems dsi) {
        this.itens = dsi;
    }
    private DefaultSelectedData opcaoSelecionada = new DefaultSelectedData();

    public DefaultSelectedData getOpcaoSelecionada() {
        return opcaoSelecionada;
    }

    public void setOpcaoSelecionada(DefaultSelectedData dsd) {
        this.opcaoSelecionada = dsd;
    }
    private DefaultSelectionItems opcoes = new DefaultSelectionItems();

    public DefaultSelectionItems getOpcoes() {
        return opcoes;
    }

    public void setOpcoes(DefaultSelectionItems dsi) {
        this.opcoes = dsi;
    }
    private HtmlInputText numNota = new HtmlInputText();

    public HtmlInputText getNumNota() {
        return numNota;
    }

    public void setNumNota(HtmlInputText hit) {
        this.numNota = hit;
    }
    private HtmlInputText campoProdutor = new HtmlInputText();

    public HtmlInputText getCampoProdutor() {
        return campoProdutor;
    }

    public void setCampoProdutor(HtmlInputText hit) {
        this.campoProdutor = hit;
    }
    private SelectInputDate campoData = new SelectInputDate();

    public SelectInputDate getCampoData() {
        return campoData;
    }

    public void setCampoData(SelectInputDate sid) {
        this.campoData = sid;
    }
    private HtmlInputText campoValor = new HtmlInputText();

    public HtmlInputText getCampoValor() {
        return campoValor;
    }

    public void setCampoValor(HtmlInputText hit) {
        this.campoValor = hit;
    }
    private DefaultSelectedData empresaSelecionada = new DefaultSelectedData();

    public DefaultSelectedData getEmpresaSelecionada() {
        return empresaSelecionada;
    }

    public void setEmpresaSelecionada(DefaultSelectedData dsd) {
        this.empresaSelecionada = dsd;
    }
    private DefaultSelectionItems empresas = new DefaultSelectionItems();

    public DefaultSelectionItems getEmpresas() {
        return empresas;
    }

    public void setEmpresas(DefaultSelectionItems dsi) {
        this.empresas = dsi;
    }
    private HtmlSelectOneMenu comboEmpresa = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboEmpresa() {
        return comboEmpresa;
    }

    public void setComboEmpresa(HtmlSelectOneMenu hsom) {
        this.comboEmpresa = hsom;
    }
    private PopupBean panelPopup1Bean = new PopupBean();

    public PopupBean getPanelPopup1Bean() {
        return panelPopup1Bean;
    }

    public void setPanelPopup1Bean(PopupBean pb) {
        this.panelPopup1Bean = pb;
    }
    private PanelPopup painelSair = new PanelPopup();

    public PanelPopup getPainelSair() {
        return painelSair;
    }

    public void setPainelSair(PanelPopup pp) {
        this.painelSair = pp;
    }
    private PopupBean panelPopup1Bean1 = new PopupBean();

    public PopupBean getPanelPopup1Bean1() {
        return panelPopup1Bean1;
    }

    public void setPanelPopup1Bean1(PopupBean pb) {
        this.panelPopup1Bean1 = pb;
    }
    private PanelPopup painelConsulta = new PanelPopup();

    public PanelPopup getPainelConsulta() {
        return painelConsulta;
    }

    public void setPainelConsulta(PanelPopup pp) {
        this.painelConsulta = pp;
    }
    private PopupBean panelPopup1Bean2 = new PopupBean();

    public PopupBean getPanelPopup1Bean2() {
        return panelPopup1Bean2;
    }

    public void setPanelPopup1Bean2(PopupBean pb) {
        this.panelPopup1Bean2 = pb;
    }
    private PanelPopup painelAviso = new PanelPopup();

    public PanelPopup getPainelAviso() {
        return painelAviso;
    }

    public void setPainelAviso(PanelPopup pp) {
        this.painelAviso = pp;
    }
    private HtmlCommandButton botaoConsultaOk = new HtmlCommandButton();

    public HtmlCommandButton getBotaoConsultaOk() {
        return botaoConsultaOk;
    }

    public void setBotaoConsultaOk(HtmlCommandButton hcb) {
        this.botaoConsultaOk = hcb;
    }
    private HtmlOutputText msgAvisoResultado = new HtmlOutputText();

    public HtmlOutputText getMsgAvisoResultado() {
        return msgAvisoResultado;
    }

    public void setMsgAvisoResultado(HtmlOutputText hot) {
        this.msgAvisoResultado = hot;
    }
    private PopupBean panelPopup1Bean3 = new PopupBean();

    public PopupBean getPanelPopup1Bean3() {
        return panelPopup1Bean3;
    }

    public void setPanelPopup1Bean3(PopupBean pb) {
        this.panelPopup1Bean3 = pb;
    }
    private PanelPopup painelMensagem = new PanelPopup();

    public PanelPopup getPainelMensagem() {
        return painelMensagem;
    }

    public void setPainelMensagem(PanelPopup pp) {
        this.painelMensagem = pp;
    }
    private CalendarConverter calendarConverter1 = new CalendarConverter();

    public CalendarConverter getCalendarConverter1() {
        return calendarConverter1;
    }

    public void setCalendarConverter1(CalendarConverter cc) {
        this.calendarConverter1 = cc;
    }
    private IntegerConverter integerConverter1 = new IntegerConverter();

    public IntegerConverter getIntegerConverter1() {
        return integerConverter1;
    }

    public void setIntegerConverter1(IntegerConverter ic) {
        this.integerConverter1 = ic;
    }
    private HtmlSelectOneRadio caixaSelecao = new HtmlSelectOneRadio();

    public HtmlSelectOneRadio getCaixaSelecao() {
        return caixaSelecao;
    }

    public void setCaixaSelecao(HtmlSelectOneRadio hsor) {
        this.caixaSelecao = hsor;
    }
    private CharacterConverter characterConverter1 = new CharacterConverter();

    public CharacterConverter getCharacterConverter1() {
        return characterConverter1;
    }

    public void setCharacterConverter1(CharacterConverter cc) {
        this.characterConverter1 = cc;
    }
    private DefaultSelectedData associadoSelecionado = new DefaultSelectedData();

    public DefaultSelectedData getAssociadoSelecionado() {
        return associadoSelecionado;
    }

    public void setAssociadoSelecionado(DefaultSelectedData dsd) {
        this.associadoSelecionado = dsd;
    }
    private DefaultSelectionItems associados = new DefaultSelectionItems();

    public DefaultSelectionItems getAssociados() {
        return associados;
    }

    public void setAssociados(DefaultSelectionItems dsi) {
        this.associados = dsi;
    }
    private DefaultSelectedData notaSelecionada = new DefaultSelectedData();

    public DefaultSelectedData getNotaSelecionada() {
        return notaSelecionada;
    }

    public void setNotaSelecionada(DefaultSelectedData dsd) {
        this.notaSelecionada = dsd;
    }
    private DefaultSelectionItems notas = new DefaultSelectionItems();

    public DefaultSelectionItems getNotas() {
        return notas;
    }

    public void setNotas(DefaultSelectionItems dsi) {
        this.notas = dsi;
    }
    private HtmlSelectOneMenu comboAssociado = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboAssociado() {
        return comboAssociado;
    }

    public void setComboAssociado(HtmlSelectOneMenu hsom) {
        this.comboAssociado = hsom;
    }
    private HtmlSelectOneMenu comboNota = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboNota() {
        return comboNota;
    }

    public void setComboNota(HtmlSelectOneMenu hsom) {
        this.comboNota = hsom;
    }
    private HtmlInputText campoIncra = new HtmlInputText();

    public HtmlInputText getCampoIncra() {
        return campoIncra;
    }

    public void setCampoIncra(HtmlInputText hit) {
        this.campoIncra = hit;
    }
    private HtmlInputText campoCpf = new HtmlInputText();

    public HtmlInputText getCampoCpf() {
        return campoCpf;
    }

    public void setCampoCpf(HtmlInputText hit) {
        this.campoCpf = hit;
    }
    private SqlDateConverter sqlDateConverter1 = new SqlDateConverter();

    public SqlDateConverter getSqlDateConverter1() {
        return sqlDateConverter1;
    }

    public void setSqlDateConverter1(SqlDateConverter sdc) {
        this.sqlDateConverter1 = sdc;
    }
    private CharacterConverter characterConverter2 = new CharacterConverter();

    public CharacterConverter getCharacterConverter2() {
        return characterConverter2;
    }

    public void setCharacterConverter2(CharacterConverter cc) {
        this.characterConverter2 = cc;
    }
    private HtmlOutputText msgAvisoPergunta = new HtmlOutputText();

    public HtmlOutputText getMsgAvisoPergunta() {
        return msgAvisoPergunta;
    }

    public void setMsgAvisoPergunta(HtmlOutputText hot) {
        this.msgAvisoPergunta = hot;
    }
    private HtmlOutputText msgMensagemResultado = new HtmlOutputText();

    public HtmlOutputText getMsgMensagemResultado() {
        return msgMensagemResultado;
    }

    public void setMsgMensagemResultado(HtmlOutputText hot) {
        this.msgMensagemResultado = hot;
    }
    private HtmlMessage msgValida4campos = new HtmlMessage();

    public HtmlMessage getMsgValida4campos() {
        return msgValida4campos;
    }

    public void setMsgValida4campos(HtmlMessage hm) {
        this.msgValida4campos = hm;
    }
    private DefaultSelectedData cpfSelecionadoConsulta = new DefaultSelectedData();

    public DefaultSelectedData getCpfSelecionadoConsulta() {
        return cpfSelecionadoConsulta;
    }

    public void setCpfSelecionadoConsulta(DefaultSelectedData dsd) {
        this.cpfSelecionadoConsulta = dsd;
    }
    private DefaultSelectionItems consultasCpf = new DefaultSelectionItems();

    public DefaultSelectionItems getConsultasCpf() {
        return consultasCpf;
    }

    public void setConsultasCpf(DefaultSelectionItems dsi) {
        this.consultasCpf = dsi;
    }
    private DefaultSelectedData incraSelecionadoConsulta = new DefaultSelectedData();

    public DefaultSelectedData getIncraSelecionadoConsulta() {
        return incraSelecionadoConsulta;
    }

    public void setIncraSelecionadoConsulta(DefaultSelectedData dsd) {
        this.incraSelecionadoConsulta = dsd;
    }
    private DefaultSelectionItems consultaIncras = new DefaultSelectionItems();

    public DefaultSelectionItems getConsultaIncras() {
        return consultaIncras;
    }

    public void setConsultaIncras(DefaultSelectionItems dsi) {
        this.consultaIncras = dsi;
    }
    private DefaultSelectedData anoSelecionadoConsulta = new DefaultSelectedData();

    public DefaultSelectedData getAnoSelecionadoConsulta() {
        return anoSelecionadoConsulta;
    }

    public void setAnoSelecionadoConsulta(DefaultSelectedData dsd) {
        this.anoSelecionadoConsulta = dsd;
    }
    private DefaultSelectionItems consultaAnos = new DefaultSelectionItems();

    public DefaultSelectionItems getConsultaAnos() {
        return consultaAnos;
    }

    public void setConsultaAnos(DefaultSelectionItems dsi) {
        this.consultaAnos = dsi;
    }
    private HtmlSelectOneMenu comboAnoConsulta = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboAnoConsulta() {
        return comboAnoConsulta;
    }

    public void setComboAnoConsulta(HtmlSelectOneMenu hsom) {
        this.comboAnoConsulta = hsom;
    }
    private HtmlSelectOneMenu comboConsultaCpf = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboConsultaCpf() {
        return comboConsultaCpf;
    }

    public void setComboConsultaCpf(HtmlSelectOneMenu hsom) {
        this.comboConsultaCpf = hsom;
    }
    private HtmlSelectOneMenu comboIncraConsulta = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboIncraConsulta() {
        return comboIncraConsulta;
    }

    public void setComboIncraConsulta(HtmlSelectOneMenu hsom) {
        this.comboIncraConsulta = hsom;
    }
    private PopupBean panelPopup1Bean4 = new PopupBean();

    public PopupBean getPanelPopup1Bean4() {
        return panelPopup1Bean4;
    }

    public void setPanelPopup1Bean4(PopupBean pb) {
        this.panelPopup1Bean4 = pb;
    }
    private PanelPopup painelValidate = new PanelPopup();

    public PanelPopup getPainelValidate() {
        return painelValidate;
    }

    public void setPainelValidate(PanelPopup pp) {
        this.painelValidate = pp;
    }


    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public NotaCadastrar() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     *
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
        int alterar = 0;

    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        itens.clear();
        itens.add(new SelectItem("Sim", "Sim"));
        itens.add(new SelectItem("Não", "Não"));

        limparCampo();
        painelSair.setRendered(false);
        painelConsulta.setRendered(false);
        painelAviso.setRendered(false);
        painelMensagem.setRendered(false);
        painelValidate.setRendered(false);
        msgMensagemResultado.setRendered(false);        




        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("NotaCadastrar Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

        empresas.clear();
        setEmpresas(new IceDecorator(EmpresaBO.class).montaCombo());
        comboEmpresa.setValue(empresaSelecionada.getSelectedObject());        

    // </editor-fold>
    // Perform application initialization that must complete
    // *after* managed components are initialized
    // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    public String botaoCancelar_action() {
        //return null means stay on the same page
        return "menu";
    }

    public String botaoSair_action() {
        painelSair.setRendered(true);
        return null;
    }

    public String botaoConsultar_action() {
        associados.clear();
        setAssociados(new IceDecorator(NotaProdutorBO.class).montaComboSemNomeRepetidoProdutor());

        notas.clear();
        notas.add(0, new SelectItem("0", "Selecione"));
        consultaAnos.clear();
        consultaAnos.add(0, new SelectItem("0", "Selecione"));
        consultasCpf.clear();
        consultasCpf.add(0, new SelectItem("0", "Selecione"));
        consultaIncras.clear();
        consultaIncras.add(0, new SelectItem("0", "Selecione"));

        limparCampo();
        painelConsulta.setRendered(true);
        return null;
    }

    public String botaoSairNao_action() {
        painelSair.setRendered(false);
        return null;
    }

    public String botaoSairSim_action() {
        //return null means stay on the same page
        return "menu";
    }

    public String botaoConsultaCancelar_action() {
        painelConsulta.setRendered(false);
        return null;
    }

    public void habilitarCampos() {
        this.campoData.setDisabled(false);
        this.campoProdutor.setDisabled(false);
        this.numNota.setDisabled(false);
        this.campoCpf.setDisabled(false);
        this.comboEmpresa.setReadonly(false);
        this.campoIncra.setDisabled(false);
        this.campoValor.setDisabled(false);
        this.caixaSelecao.setDisabled(false);
    }

    public void desabilitarCampos() {
        this.campoData.setDisabled(true);
        this.campoProdutor.setDisabled(true);
        this.numNota.setDisabled(true);
        this.campoCpf.setDisabled(true);
        this.comboEmpresa.setReadonly(true);
        this.campoIncra.setDisabled(true);
        this.campoValor.setDisabled(true);
        this.caixaSelecao.setDisabled(true);
    }

    public void limparCampo() {
        FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("NotaProdutor");
        habilitarCampos();
        campoData.resetValue();
        campoProdutor.resetValue();
        numNota.resetValue();
        campoCpf.resetValue();
        comboEmpresa.resetValue();
        campoIncra.resetValue();
        campoValor.resetValue();
        caixaSelecao.resetValue();
        parar = false;
        itemsBusca.clear();

    }
    int x;
    public String botaoConsultaOk_action() {
        try {
            String nome, nota2;
            nome = obterNomeProdutor(comboAssociado.getValue().toString());            
            nota2 = comboNota.getValue().toString();            
            NotaProdutor nota = (NotaProdutor) new IceDecorator(NotaProdutorBO.class).retornaObjeto("codNota", Integer.parseInt(comboIncraConsulta.getValue().toString()));
            x = nota.getCodNota();
            limparCampo();
            habilitarCampos();
            setBean("NotaProdutor", nota);   
            comboEmpresa.setValue(obterCodigoEmpresa(nota.getNomeEmpresa()));
            caixaSelecao.setValue(nota.getBaixaNota());            
            desabilitarCampos();            
            painelConsulta.setRendered(false);


        } catch (Exception exc) {
            painelAviso.setRendered(true);
            msgAvisoResultado.setValue("Não houve resultados para a consulta");
        }
        return null;
    }

    public String botaoAvisoSim_action() {
        painelAviso.setRendered(false);
        return null;
    }

    public String botaoNaoAviso_action() {
        painelAviso.setRendered(false);
//        painelConsulta.setRendered(false);
        return null;
    }

    public String botaoMensagemOk_action() {
        painelMensagem.setRendered(false);
        return null;
    }

    public String botaoCadastrar1_action() throws Exception {
        System.out.println ("O jaime falo que vai imprimi:"+x);
        boolean entra = false;
        if (alterar == 0){
            entra = this.validaQuatroItens();
        }
        if (alterar == 1)
        {
            entra = this.validaQuatroItensAlterar();
            if (entra==true){
                DAO dao = new DAO (NotaProdutor.class);
                Date data = (Date) campoData.getValue();
                int ano = data.getYear()+1900;
                List lista = dao.consultarQuatroItens("numNota", "cpf", "incra", "ano", numNota.getValue().toString(), campoCpf.getValue().toString(), campoIncra.getValue().toString(),ano);
                NotaProdutor np = new NotaProdutor();
                if (!(lista.isEmpty())){
                    np = (NotaProdutor) lista.get(0);
                    if (np.getCodNota() == x){
                        entra = false;
                    }
                    else{
                    painelValidate.setRendered(true);

                    }
                }
            }
        }
//         if ((alterar == 1)&&(entra = true)){
//            System.out.println ("ok");
////            if ((cpfAlterar.equals(campoCpf.getValue().toString()))&&(numNotaAlterar.equals(numNota.getValue().toString()))){
//                entra = false;
////            }
//        }

        if(entra == false){
            Date data = (Date) campoData.getValue();
            DAO dao = new DAO(NotaProdutor.class);
            nota = (NotaProdutor) getBean("NotaProdutor");
            //método para somar um dia a data cadastrada
            nota.setDataNota(new ValidaFacade().consertarData(nota.getDataNota()));
            nota.setAno(data.getYear()+1900);
            try {
                Empresa empresa = (Empresa) new IceDecorator(EmpresaBO.class).retornaObjetoCodigo("codEmpresa", Integer.parseInt(comboEmpresa.getValue().toString()));
                nota.setNomeEmpresa(empresa.getNome());
                if (caixaSelecao.getValue().toString().equals("Sim")) {
                    nota.setBaixaNota(true);
                } else {
                    nota.setBaixaNota(false);
                }
                new IceDecorator(NotaProdutorBO.class).salvar(nota);
                msgMensagemResultado.setRendered(true);
                msgAvisoResultado.setValue("Operação Efetuada Com Sucesso");
                painelMensagem.setRendered(true);
                limparCampo();
            } catch (Exception ex) {
                msgAvisoResultado.setValue("Erro Ao Efetuar Operação");
                painelMensagem.setRendered(true);
            }
        }

        return null;
    }

    public String botaoLimpar_action() {
        this.limparCampo();
        return null;
    }

    public String botaoNovo_action() {
        this.limparCampo();
        return null;
    }

    public String botaoAlterar_action() {   
        habilitarCampos();
        alterar = 1;
        return null;
    }

    

    public String obterNomeProdutor(String codNota) throws Exception {
        DAO dao = new DAO(NotaProdutor.class);
        int i = Integer.parseInt(codNota);
        List lista = dao.consultarUmItem("codNota", i);
        NotaProdutor np = (NotaProdutor) lista.get(0);
        return np.getNomeProdutor();
    }

    public int obterCodigoEmpresa(String nome) throws Exception {
        DAO dao = new DAO(Empresa.class);
        List lista = dao.consultarUmItem("nome", nome);
        Empresa empresa = (Empresa) lista.get(0);
        return empresa.getCodEmpresa();
    }

    public boolean validaQuatroItens(){
        Date data = (Date) campoData.getValue();
        int ano = data.getYear()+1900;
        DAO dao = new DAO(NotaProdutor.class);
        FacesMessage msg = new FacesMessage();
        try {
            List lista = dao.consultarQuatroItens("numNota", "incra", "cpf", "ano",
                    numNota.getValue().toString(), campoIncra.getValue().toString(), campoCpf.getValue().toString(), ano );
            if (!lista.isEmpty()) {                
                painelValidate.setRendered(true);
                return true;
            }
        } catch (Exception e) {
            throw new ValidatorException(msg);

        }
        return false;
    }
    public boolean validaQuatroItensAlterar(){
        Date data = (Date) campoData.getValue();
        int ano = data.getYear()+1900;
        DAO dao = new DAO(NotaProdutor.class);
        FacesMessage msg = new FacesMessage();
        try {
            List lista = dao.consultarQuatroItens("numNota", "incra", "cpf", "ano",
                    numNota.getValue().toString(), campoIncra.getValue().toString(), campoCpf.getValue().toString(), ano );
            if (!lista.isEmpty()) {
                    painelValidate.setRendered(false);
                    return true;
            }
        } catch (Exception e) {
            throw new ValidatorException(msg);

        }
        return false;
    }

    public void campoCpf_validate(FacesContext context, UIComponent component, Object value) {
        new ValidaFacade().validateCpf2(context, component, value);
    }
    
    public void comboAssociado_processValueChange(ValueChangeEvent vce) throws Exception {
        this.setNotas(new IceDecorator(NotaProdutorBO.class).montaComboNumNota(this.obterNomeProdutor(comboAssociado.getValue().toString())));
        notas.add(0, new SelectItem("0", "Selecione"));
        comboNota.setValue(0);
        if(comboNota.getValue().toString().equals("0")){

        }

    }

    public void comboNota_processValueChange(ValueChangeEvent vce) throws Exception {
        this.setConsultaAnos(new IceDecorator(NotaProdutorBO.class).montaComboAnoNota(this.obterNomeProdutor(comboAssociado.getValue().toString()),comboNota.getValue().toString()));
        consultaAnos.add(0, new SelectItem("0", "Selecione"));
        comboAnoConsulta.setValue(0);
    }

    public String botaoValidateOk_action() {
        painelValidate.setRendered(false);
        return null;
    }

    public void comboAnoConsulta_processValueChange(ValueChangeEvent vce) throws Exception {
        System.out.println(Integer.parseInt(comboAnoConsulta.getValue().toString()));
        this.setConsultasCpf(new IceDecorator(NotaProdutorBO.class).montaComboCPFNota(this.obterNomeProdutor(comboAssociado.getValue().toString()),comboNota.getValue().toString(),Integer.parseInt(comboAnoConsulta.getValue().toString())));
        consultasCpf.add(0, new SelectItem("0", "Selecione"));
        comboConsultaCpf.setValue(0);
    }

    public void comboConsultaCpf_processValueChange(ValueChangeEvent vce) throws Exception {
        this.setConsultaIncras(new IceDecorator(NotaProdutorBO.class).montaComboIncraNota(this.obterNomeProdutor(comboAssociado.getValue().toString()),comboNota.getValue().toString(),comboConsultaCpf.getValue().toString(),Integer.parseInt(comboAnoConsulta.getValue().toString())));
        consultaIncras.add(0, new SelectItem("0", "Selecione"));
        comboIncraConsulta.setValue(0);
    }

}
